local shuyu = fk.CreateSkill{
  name = "plum__shuyu",
}

Fk:loadTranslationTable{
  [shuyu.name] = "殊遇",
  [":"..shuyu.name] = "若你最后获得牌的方式不为摸牌，你可将一张牌当【无中生有】使用；"
.."若最后令你回复体力的角色不为自己，你可将一张牌当【桃】使用。",

  ["@$plum__shuyu"] = "殊遇",
}

local U = require "packages/utility/utility"

shuyu:addAcquireEffect(function (self, player, is_start)
  local room = player.room
  local mark = {}
  if is_start then
    mark = { "peach", "ex_nihilo" }
  else
    room.logic:getEventsByRule(GameEvent.Recover, 1, function (e)
      if e.data.who == player then
        if e.data.recoverBy and e.data.recoverBy ~= player then
          table.insert(mark, "peach")
        end
        return true
      end
    end)
    room.logic:getEventsByRule(GameEvent.MoveCards, 1, function (e)
      for _, move in ipairs(e.data) do
        if move.toArea == Card.PlayerHand and move.to == player then
          if move.moveReason ~= fk.ReasonDraw then
            table.insert(mark, "ex_nihilo")
          end
          return true
        end
      end
    end)
  end
  room:setPlayerMark(player, "@$plum__shuyu", #mark ~= 0 and mark or 0)
end)

shuyu:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@$plum__shuyu", 0)
end)

shuyu:addEffect("viewas", {
  mute = true,
  anim_type = "support",
  pattern = "peach,ex_nihilo",
  card_num = 1,
  interaction = function (self, player)
    local all_choices = { "peach", "ex_nihilo" }
    local choices = player:getViewAsCardNames(shuyu.name, player:getTableMark("@$plum__shuyu"))
    return UI.CardNameBox { choices = choices, all_choices = all_choices, }
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0
  end,
  view_as = function (self, player, cards)
    if not (#cards == 1 and self.interaction.data) then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = shuyu.name
    card:addSubcards(cards)
    return card
  end,
  before_use = function (self, player, use)
    local index = (use.card.name == "peach") and math.random(3,4) or math.random(1,2)
    player.room:notifySkillInvoked(player, shuyu.name)
    player:broadcastSkillInvoke(shuyu.name, index)
  end,
})

shuyu:addEffect(fk.HpRecover, {
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(shuyu.name, true) and player == target
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local mark = player:getTableMark("@$plum__shuyu")
    if data.recoverBy and data.recoverBy ~= player then
      table.insertIfNeed(mark, "peach")
    elseif table.contains(mark, "peach") then
      table.removeOne(mark, "peach")
    end
    room:setPlayerMark(player, "@$plum__shuyu", #mark ~= 0 and mark or 0)
  end
})

shuyu:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    if player:hasSkill(shuyu.name, true) then
      for _, move in ipairs(data) do
        if move.toArea == Card.PlayerHand and move.to == player then
          return true
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.toArea == Card.PlayerHand and move.to == player then
        local mark = player:getTableMark("@$plum__shuyu")
        if move.moveReason ~= fk.ReasonDraw  then
          table.insertIfNeed(mark, "ex_nihilo")
        elseif table.contains(mark, "ex_nihilo") then
          table.removeOne(mark, "ex_nihilo")
        end
        room:setPlayerMark(player, "@$plum__shuyu", #mark ~= 0 and mark or 0)
      end
    end
  end,
})

return shuyu